package org.iwidget.desktop.model;
import org.iwidget.desktop.ui.WidgetScroller;
import org.w3c.dom.*;
/**
 *
 * @author Muhammad Hakim A
 */

public class ElementScroller extends ElementUI
{

    public ElementScroller()
    {
        bgColor = "#FFFFFF";
        bgOpacity = 100;
        hScroller = 0;
        hDirection = 0;
        scrollSpeed = 30;
        vHeight = 500;
        vScroller = 0;
        vWidth = 500;
        name = "Scroller";
    }

    public ElementScroller(WidgetElement widget)
    {
        super(widget);
        bgColor = "#FFFFFF";
        bgOpacity = 100;
        hScroller = 0;
        hDirection = 0;
        scrollSpeed = 30;
        vHeight = 500;
        vScroller = 0;
        vWidth = 500;
        name = "Scroller";
    }

    public ElementScroller(WidgetElement widget, String name)
    {
        super(widget);
        bgColor = "#FFFFFF";
        bgOpacity = 100;
        hScroller = 0;
        hDirection = 0;
        scrollSpeed = 30;
        vHeight = 500;
        vScroller = 0;
        vWidth = 500;
        this.name = name;
    }

    public String getBgColor()
    {
        return bgColor;
    }

    public void setBgColor(String s)
    {
        if(bgColor == null || !bgColor.equals(s))
        {
            bgColor = s;
            if(myComponent != null)
            {
                WidgetScroller scroller = (WidgetScroller)myComponent;
                scroller.setColor();
            }
        }
    }

    public int getBgOpacity()
    {
        return bgOpacity;
    }

    public void setBgOpacity(int i)
    {
        if(bgOpacity != i)
        {
            bgOpacity = i;
            if(bgOpacity > 100)
                bgOpacity = 100;
            if(bgOpacity < 0)
                bgOpacity = 0;
            if(myComponent != null)
            {
                WidgetScroller scroller = (WidgetScroller)myComponent;
                scroller.setColor();
            }
        }
    }

    public int getHScroller()
    {
        return hScroller;
    }

    public void setHScroller(int i)
    {
        if(hScroller != i)
        {
            hScroller = i;
            if(myComponent != null)
            {
                WidgetScroller scroller = (WidgetScroller)myComponent;
                scroller.setHScroller(hScroller);
            }
            setChanged();
        }
    }

    public int getHDirection()
    {
        return hDirection;
    }

    public void setHDirection(int i)
    {
        hDirection = i;
    }

    public int getScrollingObject()
    {
        return hDirection;
    }

    public String getScrolling()
    {
        switch(hDirection)
        {
        case 0: // '\0'
            return "off";

        case -1: 
            return "left";

        case -2: 
            return "autoleft";

        case 1: // '\001'
            return "right";

        case 2: // '\002'
            return "autoright";
        }
        return "off";
    }

    public void setScrolling(String dir)
    {
        if(dir.equals("off"))
            hDirection = 0;
        else
        if(dir.equals("left"))
            hDirection = -1;
        else
        if(dir.equals("autoleft"))
            hDirection = -2;
        else
        if(dir.equals("right"))
            hDirection = 1;
        else
        if(dir.equals("autoright"))
            hDirection = 2;
        if(hDirection != 0 && myComponent != null)
        {
            WidgetScroller scroller = (WidgetScroller)myComponent;
            scroller.startScrolling(scrollSpeed, hDirection, 0);
        }
    }

    public void setScroll(int amount)
    {
        if(myComponent != null)
        {
            if(amount < 0)
                hDirection = 1;
            else
                hDirection = -1;
            WidgetScroller scroller = (WidgetScroller)myComponent;
            scroller.startScrolling(scrollSpeed, hDirection, 0, amount);
        }
    }

    public int getScrollSpeed()
    {
        return scrollSpeed;
    }

    public void setScrollSpeed(int i)
    {
        scrollSpeed = i;
    }

    public int getVHeight()
    {
        return vHeight;
    }

    public void setVHeight(int i)
    {
        if(vHeight != i)
        {
            vHeight = i;
            if(myComponent != null)
            {
                WidgetScroller scroller = (WidgetScroller)myComponent;
                scroller.setVSize(vWidth, vHeight);
            }
            setChanged();
        }
    }

    public int getVScroller()
    {
        if(myComponent != null)
        {
            WidgetScroller scroller = (WidgetScroller)myComponent;
            vScroller = scroller.getVScroller();
        }
        return vScroller;
    }

    public void setVScroller(int i)
    {
        if(vScroller != i)
        {
            vScroller = i;
            if(myComponent != null)
            {
                WidgetScroller scroller = (WidgetScroller)myComponent;
                scroller.setVScroller(vScroller);
            }
            setChanged();
        }
    }

    public int getVWidth()
    {
        return vWidth;
    }

    public void setVWidth(int i)
    {
        if(vWidth != i)
        {
            vWidth = i;
            if(myComponent != null)
            {
                WidgetScroller scroller = (WidgetScroller)myComponent;
                scroller.setVSize(vWidth, vHeight);
            }
            setChanged();
        }
    }

    public void parse(Node imageNode)
        throws Exception
    {
        NamedNodeMap map = imageNode.getAttributes();
        for(int iCtr = 0; iCtr < map.getLength(); iCtr++)
        {
            Node attr = map.item(iCtr);
            parseHelper(attr.getNodeName(), attr.getNodeValue(), null);
        }

        NodeList nodes = imageNode.getChildNodes();
        for(int iCtr = 0; iCtr < nodes.getLength(); iCtr++)
        {
            Node childNode = nodes.item(iCtr);
            String nodeName = childNode.getNodeName();
            String nodeValue = childNode.getNodeValue();
            if(childNode.getFirstChild() != null)
                parseHelper(nodeName, childNode.getFirstChild().getNodeValue(), childNode);
        }

        if(getName() == null || getName().length() == 0)
        {
            widget.getParent().getDebugFrame().addText("SYSTEM:Error:Scroller element is not named");
            setName("Unknown_" + System.currentTimeMillis());
        }
    }

    private void parseHelper(String nodeName, String value, Node childNode)
    {
        if(nodeName.equals("bgColor"))
            setBgColor(value);
        else
        if(nodeName.equals("bgOpacity"))
            setBgOpacity(Integer.parseInt(value));
        else
        if(nodeName.equals("cursor"))
            setCursor(value);
        else
        if(nodeName.equals("height"))
            setHeight(Integer.parseInt(value));
        else
        if(nodeName.equals("hScroller"))
            setHScroller(Integer.parseInt(value));
        else
        if(nodeName.equals("name"))
            setName(value);
        else
        if(nodeName.equals("scrolling"))
            setScrolling(value);
        else
        if(nodeName.equals("scrollSpeed"))
            setScrollSpeed(Integer.parseInt(value));
        else
        if(nodeName.equals("tooltip"))
            setTooltip(value);
        else
        if(nodeName.equals("visible"))
        {
            if(value.equals("true"))
                setVisible(true);
            else
                setVisible(false);
        } else
        if(nodeName.equals("vHeight"))
            setVHeight(Integer.parseInt(value));
        else
        if(nodeName.equals("vScroller"))
            setVScroller(Integer.parseInt(value));
        else
        if(nodeName.equals("vWidth"))
            setVWidth(Integer.parseInt(value));
        else
        if(nodeName.equals("width"))
            setWidth(Integer.parseInt(value));
        else
        if(nodeName.equals("x"))
            setX(Integer.parseInt(value));
        else
        if(nodeName.equals("y"))
            setY(Integer.parseInt(value));
        else
        if(nodeName.startsWith("on"))
            addAction(nodeName, addChildAction(nodeName, childNode));
    }

    private String bgColor;
    private int bgOpacity;
    private int hScroller;
    private int hDirection;
    private int scrollSpeed;
    private int vHeight;
    private int vScroller;
    private int vWidth;
}